package com.example.controller;

import com.example.entity.Order;
import com.example.entity.OrderType;
import com.example.entity.Passenger;
import com.example.service.OrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
//RESTful 风格的控制器，会自动将方法的返回值转换为 JSON 格式。
@RequestMapping("/api/orders")
//@RequestMapping("/api/orders")//定义了该控制器类处理的请求路径前缀为 /api/orders

public class OrderController {

    @Autowired
    //使用 @Autowired 注解将 OrderService 类注入到 OrderController 中,
    // 使得 OrderController 可以使用 OrderService 类中的方法
    private OrderService orderService;

    @Operation(summary = "创建订单", description = "根据乘客信息和订单类型创建订单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "订单创建成功",
                    content = { @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Order.class)) }),
            @ApiResponse(responseCode = "400", description = "无效的请求参数",
                    content = @Content),
            @ApiResponse(responseCode = "404", description = "未找到可用车辆",
                    content = @Content) })
    @PostMapping//定义了该方法处理的请求路径为 /api/orders，请求方法为 POST
    public Order createOrder(@RequestBody Map<String, Object> request) {
        double startLat = Double.parseDouble(request.get("startLat").toString());
        double startLng = Double.parseDouble(request.get("startLng").toString());
        double endLat = Double.parseDouble(request.get("endLat").toString());
        double endLng = Double.parseDouble(request.get("endLng").toString());
        int typeChoice = Integer.parseInt(request.get("typeChoice").toString());
        OrderType orderType = OrderType.values()[typeChoice - 1];
        
        // 获取距离和时长参数
        double distance = 0;
        int duration = 0;
        if (request.containsKey("distance")) {
            distance = Double.parseDouble(request.get("distance").toString());
        }
        if (request.containsKey("duration")) {
            duration = Integer.parseInt(request.get("duration").toString());
        }
        
        // 获取用户ID作为乘客姓名
        String userId = request.get("userId").toString();
        
        System.out.println("OrderController类：接受订单时订单类型：" + orderType);
        return orderService.createOrder(userId, orderType, startLat, startLng, endLat, endLng, distance, duration);
    }

    @Operation(summary = "根据订单 ID 获取订单信息", description = "通过订单 ID 查询订单详细信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取订单信息",
                    content = { @Content(mediaType = "application/json",
                            schema = @Schema(implementation = Order.class)) }),
            @ApiResponse(responseCode = "404", description = "未找到该订单",
                    content = @Content) })
    @GetMapping("/{orderId}")//定义了该方法处理的请求路径为 /api/orders/{orderId}，请求方法为 GET
    //@PathVariable 注解用于将请求路径中的占位符值绑定到方法的参数中
    public Order getOrderById(@Parameter(description = "订单 ID") @PathVariable String orderId) {
        return orderService.getOrderById(orderId);
    }

    @Operation(summary = "获取用户历史订单", description = "根据用户ID获取该用户的所有历史订单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取历史订单"),
            @ApiResponse(responseCode = "404", description = "未找到该用户的历史订单",
                    content = @Content)
    })
    @GetMapping("/history/{userId}")
    public List<Order> getOrderHistoryByUserId(@Parameter(description = "用户ID") @PathVariable String userId) {
        // 这里假设有对应的服务方法可以获取历史订单
        // 实际项目中需要实现对应的服务方法
        return orderService.getOrdersByUserId(userId);
    }
    
    @Operation(summary = "获取所有订单", description = "管理员获取系统中的所有订单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功获取所有订单"),
            @ApiResponse(responseCode = "403", description = "权限不足",
                    content = @Content)
    })
    @GetMapping("/all")
    public List<Order> getAllOrders() {
        // 调用服务方法获取所有订单
        return orderService.getAllOrders();
    }
}